package com.ruoyix.flowable.controller;

import com.ruoyix.flowable.domain.vo.BpmTaskApproveVo;
import lombok.RequiredArgsConstructor;

import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyix.common.annotation.RepeatSubmit;
import com.ruoyix.common.annotation.Log;
import com.ruoyix.common.core.controller.BaseController;
import com.ruoyix.common.core.domain.PageQuery;
import com.ruoyix.common.core.domain.R;
import com.ruoyix.common.enums.BusinessType;
import com.ruoyix.flowable.domain.vo.BpmProcessInstanceVo;
import com.ruoyix.flowable.domain.bo.BpmProcessInstanceBo;
import com.ruoyix.flowable.service.IBpmProcessInstanceService;
import com.ruoyix.common.core.page.TableDataInfo;

import java.util.Map;

/**
 * 流程实例
 *
 * @author ruoyi
 * @date 2023-11-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/flowable/bpmProcessInstance")
public class BpmProcessInstanceController extends BaseController {

    private final IBpmProcessInstanceService iBpmProcessInstanceService;

    /**
     * 查询流程实例列表
     */
    @SaCheckPermission("flowable:bpmProcessInstance:list")
    @GetMapping("/list")
    public TableDataInfo<BpmProcessInstanceVo> list(BpmProcessInstanceBo bo, PageQuery pageQuery) {
        return iBpmProcessInstanceService.queryPageList(bo, pageQuery);
    }



    /**
     * 查询流程实例列表
     */
    @SaCheckPermission("flowable:bpmProcessInstance:list")
    @GetMapping("/finishList")
    public TableDataInfo<BpmProcessInstanceVo> finishList(BpmProcessInstanceBo bo, PageQuery pageQuery) {
        return iBpmProcessInstanceService.finishList(bo, pageQuery);
    }



    /**
     * 获取流程实例详细信息
     *
     * @param processInstanceId 主键
     */
    @SaCheckPermission("flowable:bpmProcessInstance:query")
    @GetMapping("/{processInstanceId}")
    public R<Map<String, Object>> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String processInstanceId) {
        return R.ok(iBpmProcessInstanceService.queryById(processInstanceId));
    }

    /**
     * 新增流程实例
     */
    @SaCheckPermission("flowable:bpmProcessInstance:add")
    @Log(title = "流程实例", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @GetMapping("/add")
    public R<BpmTaskApproveVo> add(String processDefinitionId) {
        return R.ok(iBpmProcessInstanceService.addProcessInstance(processDefinitionId));
    }


    /**
     * 删除流程任务信息
     *
     * @param processInstanceId 流程实例ID
     */
    @SaCheckPermission("flowable:bpmProcessInstance:remove")
    @Log(title = "删除历史实例", businessType = BusinessType.DELETE)
    @GetMapping("/deleteHistoryProcessInstance")
    public R<Void> deleteHistoryProcessInstance( String processInstanceId) {
        return toAjax(iBpmProcessInstanceService.deleteHistoryProcessInstance(processInstanceId));
    }

    /**
     * 删除流程任务信息
     *
     * @param processInstanceId 流程实例ID
     */
    @SaCheckPermission("flowable:bpmProcessInstance:remove")
    @Log(title = "删除流程实例", businessType = BusinessType.DELETE)
    @GetMapping("/deleteProcessInstance")
    public R<Void> deleteProcessInstance( String processInstanceId) {
        return toAjax(iBpmProcessInstanceService.deleteProcessInstance(processInstanceId));
    }

}
